package com.happypets.backstage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.happypets.model.ManagerBean;
import com.happypets.servicesImpl.ManagerServiceImpl;
import com.happypets.servicesImpl.UserServiceImpl;

/**
 * Servlet implementation class BLogin
 */
public class BLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		UserServiceImpl psi = new UserServiceImpl();
		
		ManagerServiceImpl msi = new ManagerServiceImpl();
		
		HttpSession session = request.getSession();
		
		
		
		String userName = request.getParameter("name");
		
		String passwd = request.getParameter("password");
		
		String id = psi.backstageUserLogin(userName, passwd);
		//failed
		PrintWriter out = response.getWriter();
		
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		Map<String,String> m = new HashMap<String,String>();
		
		if(id.equals("-1")){
			
			
			
			
			m.put("msg", "账号或密码错误！");
			

			
			
		}else{
			
			
			
			ManagerBean mb = msi.managerInfo(id);
			
			session.setAttribute("manager", mb);
			
			m.put("msg", "登陆成功！");
		}
		
		list.add(m);
		
		Gson gson=new Gson();
		String json=gson.toJson(list);
//		System.out.println(json);
		out.write(json);
		out.flush();
		out.close();


	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
